package com.ajie.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.spi.service.contexts.SecurityContext;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口文档配置类
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Slf4j
public class SwaggerConfig {

    /**
     * 创建接口文档
     * @return
     */
    @Bean
    public Docket createApi(){
        log.info("该文件名是：SwaggerConfig,该方法是：createApi");
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ajie.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());

    }

    /**
     * 设置文档信息
     * @return
     */
    public ApiInfo apiInfo(){
        log.info("该文件名是：SwaggerConfig,该方法是：apiInfo");
        return new ApiInfoBuilder()
                .title("农业教育平台")
                .version("1.0.0")
                .contact(new Contact("阿杰" ,"http://localhost:8080/doc.html","ajie2712@gmail.com"))
                .description("农业教育平台接口文档")
                .build();
    }

    /**
     * 设置请求信息
     * @return
     */
    public List<ApiKey> securitySchemes(){
        log.info("该文件名是：SwaggerConfig,该方法是：securitySchemes");
        List<ApiKey> list = new ArrayList<>();
        ApiKey key = new ApiKey("Authorization","Authorization","Header");
        list.add(key);
        return list;
    }

    /**
     * 配置security对swagger测试的
     * @return
     */
    public List<SecurityContext> securityContexts(){
        log.info("该文件名是：SwaggerConfig,该方法是：securityContexts");
        List<SecurityContext> list = new ArrayList<>();
        list.add(getSecurityContext());
        return list;
    }

    /**
     * 得到授权路径
     * @return
     */
    private SecurityContext getSecurityContext(){
        log.info("该文件名是：SwaggerConfig,该方法是：getSecurityContext");
        return SecurityContext
                .builder()
                .securityReferences(securityReferences())
                .forPaths(PathSelectors.regex("hello/.*"))
                .build();
    }

    /**
     * 给授权swagger，可以进行接口测试
     * @return
     */
    private List<SecurityReference>  securityReferences(){
        log.info("该文件名是：SwaggerConfig,该方法是：securityReferences");
    
        List<SecurityReference> list = new ArrayList<>();

        //授权范围，全局
        AuthorizationScope scope = new AuthorizationScope("global" , "accessEverything");
        AuthorizationScope[] scopes = new AuthorizationScope[1];
        scopes[0] = scope;
        list.add(new SecurityReference("Authorization",scopes));
        return list;

    }
}
